package com.topdt.jbpm.form;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thx.common.spring.BeanFactory;
import com.thx.common.util.ThreadLocalContext;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.identity.User;
import com.topdt.jbpm.model.Application;
import com.topdt.jbpm.model.Control;
import com.topdt.jbpm.model.Field;
import com.topdt.jbpm.model.Form;
import com.topdt.jbpm.model.Work;
import com.topdt.jbpm.service.ControlService;
import com.topdt.jbpm.service.FieldService;


/**
 *  @Project       : saasoa
 *  @Program Name  : com.topdt.jbpm.form.AbstractGeneralFormHandle.java
 *  @Class Name    : AbstractGeneralFormHandle
 *  @Description   : 抽象通用表单处理
 *  @Author        : calonlan
 *  @Creation Date : 2015年3月10日 下午6:49:56
 *  @ModificationHistory
 *  Who        When          What
 *  --------   ----------    -----------------------------------
 *  username   2015年3月10日       TODO
 */
public abstract class AbstractGeneralFormHandle extends AbstractFormHandle{

	public FormUI assemble(Map<String, Object> context) {

		Application app=(Application)context.get("appObj");

		User user = (User)context.get("userObj");
		context.put("currentUserName", user.getName());
		context.put("sessionId",(String)ThreadLocalContext.get(Constants.SESSIONID_KEY));
		context.put("appName", app.getAppName());
		if(Application.APPATTR_DOC.equals(app.getAppAttr()))
			context.put("body", "");


		Work work = (Work)context.get("workObj");

		if(work.isFreeFlow())
			context.put("freeFlow", "true");
		Form form = app.getForm();

		Document doc = getDocument(work.getWorkId());

		ControlService controlService = (ControlService) BeanFactory.getBean("controlService");

		String activity=(String)context.get("activity");


		Object[] objs = new Object[] { app.getAppId(), work.getFlowId(), activity, form.getFormId() };
		Control control = controlService.getControlByObjects(objs);

		if (control == null) {
			return getFormUI(app, null, null, doc, context, work);
		}

		Map<String, String> fieldControl = new HashMap<String, String>();

		FieldService fieldService = (FieldService) BeanFactory.getBean("fieldService");
		List<Field> fields = fieldService.getFieldsByformId(form.getFormId());

		if(fields==null)
			return getFormUI(app, null, null, null,context, work);

		for (int i = 0; i < fields.size(); i++) {
			Field field = fields.get(i);
			fieldControl.put(field.getFieldName(), String.valueOf(control.getFieldControl().charAt(i)));
		}

		if (fieldControl.size() == 0)
			fieldControl = null;

		//把流程操作权限放到context
		Map<String, String> funControl = new HashMap<String, String>();
		String[][] menuItems = FormMenuDefin.getMenuItems(app.getAppAttr());
		for (int i = 0; i < menuItems.length; i++) {
			String[] item = menuItems[i];
			//weboffice编辑文档的按钮控制
			if(item[1].equals("编辑正文")) {
				context.put("docControl", control.getFunctionControl().substring(i));
			}
			funControl.put(item[0], String.valueOf(control.getFunctionControl().charAt(i)));
		}
		if (funControl.size() == 0)
			funControl = null;

		return getFormUI(app, fieldControl, funControl, doc, context, work);
	}

}
